package com.example.demo.controller;


import com.example.demo.model.CourseSection;
import com.example.demo.security.CustomUserDetails;
import com.example.demo.service.CourseUploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher/courses")
@RequiredArgsConstructor
public class TeacherCourseController {
    private final CourseUploadService courseUploadService;

    @PostMapping("/upload")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<CourseSection> uploadCourseMaterials(
            @ModelAttribute CourseUpload upload,
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        Long teacherId = userDetails.getId();
        return ResponseEntity.ok(courseUploadService.uploadCourseMaterials(upload , teacherId));
    }

    @GetMapping("/{courseId}/sections")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<List<CourseSection>> getCourseSections(
            @PathVariable Long courseId,
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        Long teacherId = userDetails.getId();
        return ResponseEntity.ok(courseUploadService.getCourseSections(courseId, teacherId));
    }

    @DeleteMapping("/sections/{sectionId}")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<?> deleteSection(
            @PathVariable Long sectionId,
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        Long teacherId = userDetails.getId();
        courseUploadService.deleteSection(sectionId, teacherId);
        return ResponseEntity.noContent().build();
    }
}